/**
 * 
 */
package org.bankabeule.ui.actions;

import org.bankabeule.core.ModelManager;
import org.bankabeule.ui.Activator;
import org.bankabeule.ui.utils.Logger;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.LegacyActionTools;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;

/**
 * @author sdudoit
 * 
 */
public class OpenAction extends Action {

	
	public OpenAction() {
		super();
		setText("&Open");
		setId(getClass().getCanonicalName());
		setImageDescriptor(Activator.getImageDescriptor("OpenFile.gif"));
		setAccelerator(LegacyActionTools.convertAccelerator("CTRL+O"));
	}
	
	
	@Override
	public void run() {
		try {
			FileDialog dialog = new FileDialog(Display.getCurrent()
					.getActiveShell(), org.eclipse.swt.SWT.OPEN);
			dialog.setFilterExtensions(new String[]{ModelManager.FILE_EXTENSION});
			String path = dialog.open();
			if (path != null) {
				if (ModelManager.get().isDirty()) {
					boolean mustSave = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), 
							"Confirmation", "Do you want to save before closing ?");
					if (mustSave) {
						ModelManager.get().save();
					}
				}
				ModelManager.get().load(path);
			}
		} catch (Exception e) {
			Logger.log(e);
		}
	}

	
}
